import os
import sys
from bs4 import BeautifulSoup
from markdownify import markdownify as md
import argparse

def convert_html_to_markdown(html_filepath):
    """
    Reads an HTML file, extracts the content within the <main> tag,
    and converts it to Markdown.

    Args:
        html_filepath (str): The path to the input HTML file.

    Returns:
        str: The Markdown content, or None if an error occurs or <main> tag is not found.
    """
    try:
        # Read the HTML file content
        with open(html_filepath, 'r', encoding='utf-8') as f:
            html_content = f.read()

        # Parse the HTML using BeautifulSoup
        soup = BeautifulSoup(html_content, 'lxml') # Using lxml parser

        # Find the <main> tag
        main_content = soup.find('main')

        if not main_content:
            print(f"警告：在 '{html_filepath}' 中找不到 <main> 标签，跳过此文件。")
            return None

        # Convert the main content to Markdown
        markdown_content = md(str(main_content), heading_style='ATX')
        return markdown_content

    except Exception as e:
        print(f"处理文件 '{html_filepath}' 时发生错误：{e}")
        return None

def process_directory(source_dir, target_dir):
    """
    Recursively traverses the source directory, converts HTML files to Markdown,
    and saves them to the target directory, preserving the structure.

    Args:
        source_dir (str): The path to the source directory containing HTML files.
        target_dir (str): The path to the target directory where Markdown files will be saved.
    """
    source_dir = os.path.abspath(source_dir)
    target_dir = os.path.abspath(target_dir)

    if not os.path.isdir(source_dir):
        print(f"错误：源文件夹 '{source_dir}' 不存在或不是一个有效的目录。")
        return

    print(f"开始处理源文件夹：'{source_dir}'")
    print(f"Markdown 文件将保存到：'{target_dir}'")

    converted_count = 0
    skipped_count = 0

    for root, _, files in os.walk(source_dir):
        for filename in files:
            if filename.lower().endswith(".html"):
                html_filepath = os.path.join(root, filename)
                print(f"  找到 HTML 文件：'{html_filepath}'")

                # Convert HTML to Markdown
                markdown_content = convert_html_to_markdown(html_filepath)

                if markdown_content is not None:
                    # Calculate relative path from source_dir
                    relative_path = os.path.relpath(html_filepath, source_dir)
                    # Construct target path
                    md_filename = os.path.splitext(filename)[0] + ".md"
                    target_subpath = os.path.dirname(relative_path)
                    target_md_dir = os.path.join(target_dir, target_subpath)
                    target_md_filepath = os.path.join(target_md_dir, md_filename)

                    try:
                        # Create target directory if it doesn't exist
                        os.makedirs(target_md_dir, exist_ok=True)

                        # Write the Markdown content to the output file
                        with open(target_md_filepath, 'w', encoding='utf-8') as f:
                            f.write(markdown_content)
                        print(f"    -> 已转换并保存到：'{target_md_filepath}'")
                        converted_count += 1
                    except Exception as e:
                        print(f"    错误：无法写入 Markdown 文件 '{target_md_filepath}'：{e}")
                        skipped_count += 1
                else:
                    skipped_count += 1

    print(f"\n处理完成。")
    print(f"成功转换文件数：{converted_count}")
    print(f"跳过或失败文件数：{skipped_count}")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="批量将指定文件夹下的 HTML 文件转换为 Markdown 文件，提取 <main> 标签内容。")
    parser.add_argument("source_dir", help="包含 HTML 文件的源文件夹路径。")
    parser.add_argument("target_dir", help="保存 Markdown 文件的目标文件夹路径。")

    args = parser.parse_args()

    process_directory(args.source_dir, args.target_dir)
